package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.RoleRightsDao;
import cn.ym.mall.entity.Role;
import cn.ym.mall.entity.RoleRights;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class RoleRightsDaoImpl implements RoleRightsDao {
    QueryRunner queryRunner =new QueryRunner();
    /**
     * 获得所有角色权限
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<RoleRights> getAll() throws SQLException {
        String sql="select rid, rigid from ym_rolerights;";
        List<RoleRights> roleRightsList=null;
        try{
            roleRightsList= queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<RoleRights>(RoleRights.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return roleRightsList;
    }

    /**
     * 添加新角色权限
     *
     * @param roleRights
     * @return
     * @throws SQLException
     */
    @Override
    public int add(RoleRights roleRights) throws SQLException {
        String sql="insert into ym_rolerights(rid,rigid) value(?,?);";
        Object[]  arr={roleRights.getRigid()};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据角色Id删除角色权限
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public int delete(Integer rid) throws SQLException {
        String sql="delete from ym_rolerights where rid=?;";
        Object[]  arr={rid};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 更新角色权限
     *
     * @param roleRights
     * @return
     * @throws SQLException
     */
    @Override
    public int update(RoleRights roleRights) throws SQLException {
        String sql="update ym_rolerights set rigid=? where rid=?;";
        Object[]  arr={roleRights.getRigid(),roleRights.getRid()};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据角色id获得角色权限
     *
     * @return
     * @throws SQLException
     */
    @Override
    public RoleRights getRoleRightsByRid(Integer rid) throws SQLException {
        RoleRights roleRights=null;
        try{
            String sql="select rid, rigid from ym_rolerights where rid=?;";
            roleRights= queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<RoleRights>(RoleRights.class),rid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return roleRights;
    }
}
